package com.ie.cloud.payment.service.impl.listener;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ie.cloud.payment.cache.RedisUtil;
import com.ie.cloud.payment.constants.CS;
import com.ie.cloud.payment.dto.event.CallbackMsgEvent;
import com.ie.cloud.payment.service.impl.PayOrderFlowRecordServiceImpl;
import com.ie.cloud.payment.service.impl.PayOrderService;
import com.ie.cloud.payment.utils.IdUtils;
import com.ie.cloud.payment.vo.PayOrderPartVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * Created with FR
 *
 * @Author: DQ
 * @Date: 2025/07/24/11:03
 * @Description: 监听回调信息
 */
@Service
@Slf4j
public class CallbackMessageListenerService {
    /**
     * 异步处理线程池
     */
    private final static ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(10);



    @Resource
    private PayOrderFlowRecordServiceImpl payOrderFlowRecordService;

    @Resource
    private PayOrderService payOrderService;

    /**
     * 监听报文信息 并更新订单
     * @param event 回调信息
     */
    @EventListener
    public void subscribeMsgUpdateOrder(CallbackMsgEvent event) {
        log.info("收到事件,事件名称tag{},事件内容 {}",event.eventTag(),event.getSource());


        log.info("开始处理事件=================>");


        RedisUtil.setObject(StrUtil.join(StrUtil.COLON,CS.EVENT_CACHE.EVENT_ICBC_CALL_MSG,IdUtils.getId()),event.getSource());


        scheduledThreadPool.submit(() ->{
            try {
                log.info("开始处理回调支付报文=================>");

                PayOrderPartVo payOrderPartVo = parseFromJson(event.getSource().toString());

                payOrderFlowRecordService.pullFlowRecord(
                        payOrderPartVo.getTrxDate(),
                        payOrderPartVo.getBusiCode(),
                        payOrderPartVo.getAppId());


                payOrderService.updateToSuccessOrder(payOrderPartVo.getTrxDate());
            }catch (Exception e) {
                log.error("订单明细查询 错误: {}",e.getMessage());
            }
        });




    }

    public static PayOrderPartVo parseFromJson(String json) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(json);

        PayOrderPartVo record = new PayOrderPartVo();

        String appId = rootNode.get("appId").asText();

        // 从 PUBLIC 中获取 trxDate
        JsonNode publicNode = rootNode.get("APPROOT").get("PUBLIC");
        String trxDateStr = publicNode.get("trxDate").asText();
        String trxTime = publicNode.get("trxTime").asText();
        record.setTrxDate(trxDateStr);
        record.setTrxTime(trxTime);

        // 从 PRIVATE 中获取数据
        JsonNode privateNode = rootNode.get("APPROOT").get("PRIVATE");
        // 从 PRIVATE.List 中获取数据
        JsonNode listNode = privateNode.get("List");
        record.setBusiCode(listNode.get("busiCode").asText());
        record.setAppId(appId);
        record.setPayAmount(listNode.get("payAmount").asText());
        record.setPaymentChannel(listNode.get("channelType").asText());
        record.setTradeType("1");
        record.setExtra(json);
        return record;
    }

    public static void main(String[] args) throws Exception {
        String str = "{\n" +
                "    \"APPROOT\": {\n" +
                "        \"PUBLIC\": {\n" +
                "            \"bizCode\": \"omcp5901\",\n" +
                "            \"trxTime\": \"11:47:48\",\n" +
                "            \"channelType\": \"\",\n" +
                "            \"trxDate\": \"2025-07-24\"\n" +
                "        },\n" +
                "        \"PRIVATE\": {\n" +
                "            \"trxsqnb\": \"63010\",\n" +
                "            \"payRefGuid\": \"P20250724M000000003088114024\",\n" +
                "            \"bankFlag\": \"0\",\n" +
                "            \"totalPayAmt\": \"1\",\n" +
                "            \"trxZone\": \"01001\",\n" +
                "            \"payAccount\": \"6222031001051689003\",\n" +
                "            \"custName\": \"段清\",\n" +
                "            \"chanSerno\": \"030310015205872753345719946\",\n" +
                "            \"telNo\": \"00059\",\n" +
                "            \"listNum\": \"1\",\n" +
                "            \"trxsqns\": \"474\",\n" +
                "            \"chanType\": \"303\",\n" +
                "            \"List\": {\n" +
                "                \"item2\": \"\",\n" +
                "                \"item1\": \"\",\n" +
                "                \"item39\": \"\",\n" +
                "                \"item38\": \"\",\n" +
                "                \"item37\": \"\",\n" +
                "                \"item36\": \"\",\n" +
                "                \"channelType\": \"1\",\n" +
                "                \"item35\": \"\",\n" +
                "                \"item34\": \"\",\n" +
                "                \"item33\": \"\",\n" +
                "                \"item32\": \"\",\n" +
                "                \"item31\": \"\",\n" +
                "                \"item30\": \"\",\n" +
                "                \"payAmount\": \"1\",\n" +
                "                \"payTimeStamp\": \"2025-07-24 11:46:44\",\n" +
                "                \"item29\": \"\",\n" +
                "                \"item28\": \"\",\n" +
                "                \"item27\": \"\",\n" +
                "                \"item26\": \"\",\n" +
                "                \"item25\": \"\",\n" +
                "                \"item24\": \"\",\n" +
                "                \"item23\": \"\",\n" +
                "                \"item22\": \"\",\n" +
                "                \"item21\": \"\",\n" +
                "                \"corpCis\": \"051290000686540\",\n" +
                "                \"item20\": \"\",\n" +
                "                \"query_item15\": \"\",\n" +
                "                \"query_item14\": \"\",\n" +
                "                \"query_item13\": \"\",\n" +
                "                \"query_item12\": \"\",\n" +
                "                \"query_item11\": \"\",\n" +
                "                \"query_item10\": \"\",\n" +
                "                \"projectId\": \"PJ140018065070317309\",\n" +
                "                \"transRefGUID\": \"E3032100020250724K097338930\",\n" +
                "                \"item19\": \"\",\n" +
                "                \"item18\": \"\",\n" +
                "                \"item17\": \"\",\n" +
                "                \"item16\": \"\",\n" +
                "                \"item15\": \"\",\n" +
                "                \"item14\": \"\",\n" +
                "                \"item13\": \"\",\n" +
                "                \"item12\": \"\",\n" +
                "                \"item11\": \"\",\n" +
                "                \"item10\": \"\",\n" +
                "                \"item50\": \"\",\n" +
                "                \"busiCode\": \"194822813374559436\",\n" +
                "                \"item49\": \"\",\n" +
                "                \"item48\": \"\",\n" +
                "                \"item47\": \"\",\n" +
                "                \"item46\": \"\",\n" +
                "                \"item45\": \"\",\n" +
                "                \"item44\": \"\",\n" +
                "                \"item43\": \"\",\n" +
                "                \"item42\": \"\",\n" +
                "                \"item41\": \"\",\n" +
                "                \"item40\": \"\",\n" +
                "                \"query_item1\": \"\",\n" +
                "                \"query_item7\": \"\",\n" +
                "                \"item8\": \"\",\n" +
                "                \"query_item6\": \"\",\n" +
                "                \"item7\": \"\",\n" +
                "                \"query_item9\": \"\",\n" +
                "                \"query_item8\": \"\",\n" +
                "                \"item9\": \"\",\n" +
                "                \"query_item3\": \"\",\n" +
                "                \"item4\": \"\",\n" +
                "                \"query_item2\": \"\",\n" +
                "                \"item3\": \"\",\n" +
                "                \"query_item5\": \"\",\n" +
                "                \"item6\": \"\",\n" +
                "                \"payDate\": \"2025-07-24\",\n" +
                "                \"query_item4\": \"\",\n" +
                "                \"item5\": \"\"\n" +
                "            },\n" +
                "            \"trxBrno\": \"06889\"\n" +
                "        }\n" +
                "    },\n" +
                "    \"appId\": \"11000000000000060923\"\n" +
                "}";


        System.out.println(parseFromJson( str));
    }

}
